<?php
class hrd_karyawan_model extends MY_Model {
	var $nama_tabel = 'hrd_karyawan';
	
	function GetJSON($status) {
		if($status == 2) { 
			// Untuk Menampilkan seluruh karyawan pada menu absen karyawan
			$ls_sql	= "	SELECT	a.*, b.nama_jabatan, c.nama_departemen, d.nama_sub_departemen, e.gaji, f.nama_cost_center, g.nama_grub_absen, h.id_status_kontrak_fk, j.nama_kontrak
					FROM	".$this->nama_tabel." a
					LEFT JOIN HRD_JABATAN b ON a.id_jabatan_fk = b.id_jabatan_pk
					LEFT JOIN HRD_DEPARTEMEN c ON a.id_departemen_fk = c.id_departemen_pk
					LEFT JOIN HRD_SUB_DEPARTEMEN d ON a.id_sub_departemen_fk = d.id_sub_departemen_pk
					LEFT JOIN HRD_POIN_GAJI e ON a.id_poin_gaji_fk = e.id_poin_gaji_pk
					LEFT JOIN SW_COST_CENTER f ON a.id_cost_center_fk = f.id_cost_center_pk
					LEFT JOIN HRD_GRUB_ABSEN G ON a.id_grub_absen_fk = g.id_grub_absen_pk
					LEFT JOIN HRD_DETAIL_KONTRAK H ON a.id_karyawan_pk = h.id_karyawan_fk
					LEFT JOIN HRD_STATUS_KONTRAK j ON h.id_status_kontrak_fk = j.id_status_kontrak_pk
					WHERE	(UPPER(id_karyawan_pk) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_karyawan) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(NVL(nama_jabatan,'')) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'id_karyawan_pk' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
		} else {
			// Menampilkan karyawan berdasarkan jenis Organik atau kontrak pekerjaan
			$ls_sql	= "	SELECT	a.*, b.nama_jabatan, c.nama_departemen, d.nama_sub_departemen, e.gaji, f.nama_cost_center, g.nama_grub_absen
					FROM	".$this->nama_tabel." a
					LEFT JOIN HRD_JABATAN b ON a.id_jabatan_fk = b.id_jabatan_pk
					LEFT JOIN HRD_DEPARTEMEN c ON a.id_departemen_fk = c.id_departemen_pk
					LEFT JOIN HRD_SUB_DEPARTEMEN d ON a.id_sub_departemen_fk = d.id_sub_departemen_pk
					LEFT JOIN HRD_POIN_GAJI e ON a.id_poin_gaji_fk = e.id_poin_gaji_pk
					LEFT JOIN SW_COST_CENTER f ON a.id_cost_center_fk = f.id_cost_center_pk
					LEFT JOIN HRD_GRUB_ABSEN G ON a.id_grub_absen_fk = g.id_grub_absen_pk
					WHERE	(UPPER(id_karyawan_pk) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_karyawan) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(NVL(nama_jabatan,'')) LIKE '%".strtoupper(post('keyword'))."%')
							AND a.status_kontrak_pekerjaan = '".$status."'
					ORDER BY ".(post('sort')=='' ? 'id_karyawan_pk' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
		}
					
					
		return json_encode( $this->paging($ls_sql) );
		// print_r($ls_sql);
	}

	function Add($arr_data){
		$this->db->insert($this->nama_tabel, $arr_data);
	}

	function Update($arr_data, $arr_rule){
		// print_r($arr_data);
		$this->db->update($this->nama_tabel, $arr_data, $arr_rule);
	}

	function DeleteSingle($arr_rule) {
		try {
			$this->db->delete($this->nama_tabel, array('id_karyawan_pk' => $arr_rule['id_karyawan_pk']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}

	function GetSingleView($id_karyawan_pk="kry001") {
		$result	= $this->db->query("SELECT	a.*, TO_CHAR(a.tgl_bergabung,'DD/MM/YYYY') tgl_bergabung,
									b.nama_jabatan, c.nama_lokasi, TO_CHAR(a.tgl_lahir,'DD/MM/YYYY') tgl_lahir, TO_CHAR(d.tgl_mulai,'DD/MM/YYYY') tgl_mulai, TO_CHAR(d.tgl_selesai,'DD/MM/YYYY') tgl_selesai, d.ket_detail_kontrak, d.id_detail_kontrak_pk, d.id_status_kontrak_fk, e.gaji
									FROM	HRD_KARYAWAN a
									LEFT JOIN HRD_JABATAN b ON a.id_jabatan_fk = b.id_jabatan_pk
									LEFT JOIN HRD_LOKASI c ON a.id_lokasi_fk = c.id_lokasi_pk
									LEFT JOIN HRD_DETAIL_KONTRAK d ON a.id_karyawan_pk = d.id_karyawan_fk
									LEFT JOIN HRD_POIN_GAJI e ON a.id_poin_gaji_fk = e.id_poin_gaji_pk
									WHERE	a.id_karyawan_pk = '".strtoupper($id_karyawan_pk)."'"
									);


		if($result->num_rows() == 1) {
			return $result->row_array();
		} else {
			return NULL;
		}

	}

}
?>